<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>Python迭代器及生成器的简单理解 | 小豆子的Blog</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="迭代器&amp;emsp;&amp;emsp;在使用列表时，直接将数据存入列表将会占据大量空间，且复用率较低，为解决这个问题，这里了解一下迭代器，从而创建一种数据产生的方式，以此来节省空间。 迭代对象&amp;emsp;&amp;emsp;注意，这里需要使用到内建函数iter，简单理解为，使用了iter才会是一个可迭代对象，关于这部分，我们可以对一些对象做一些判断，从而清楚是不是可迭代对象，比如L1列表： 1234from co">
<meta property="og:type" content="article">
<meta property="og:title" content="Python迭代器及生成器的简单理解">
<meta property="og:url" content="http://yoursite.com/2020/06/02/Python%E8%BF%AD%E4%BB%A3%E5%99%A8%E5%8F%8A%E7%94%9F%E6%88%90%E5%99%A8%E7%9A%84%E7%AE%80%E5%8D%95%E7%90%86%E8%A7%A3/index.html">
<meta property="og:site_name" content="小豆子的Blog">
<meta property="og:description" content="迭代器&amp;emsp;&amp;emsp;在使用列表时，直接将数据存入列表将会占据大量空间，且复用率较低，为解决这个问题，这里了解一下迭代器，从而创建一种数据产生的方式，以此来节省空间。 迭代对象&amp;emsp;&amp;emsp;注意，这里需要使用到内建函数iter，简单理解为，使用了iter才会是一个可迭代对象，关于这部分，我们可以对一些对象做一些判断，从而清楚是不是可迭代对象，比如L1列表： 1234from co">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-06-02T00:36:31.000Z">
<meta property="article:modified_time" content="2020-06-02T00:39:04.051Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="小豆子的Blog" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 4.2.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">小豆子的Blog</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-Python迭代器及生成器的简单理解" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/06/02/Python%E8%BF%AD%E4%BB%A3%E5%99%A8%E5%8F%8A%E7%94%9F%E6%88%90%E5%99%A8%E7%9A%84%E7%AE%80%E5%8D%95%E7%90%86%E8%A7%A3/" class="article-date">
  <time datetime="2020-06-02T00:36:31.000Z" itemprop="datePublished">2020-06-02</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      Python迭代器及生成器的简单理解
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="迭代器"><a href="#迭代器" class="headerlink" title="迭代器"></a>迭代器</h2><p>&emsp;&emsp;在使用列表时，直接将数据存入列表将会占据大量空间，且复用率较低，<br>为解决这个问题，这里了解一下迭代器，从而创建一种数据产生的方式，以此来节省空间。</p>
<h2 id="迭代对象"><a href="#迭代对象" class="headerlink" title="迭代对象"></a>迭代对象</h2><p>&emsp;&emsp;注意，这里需要使用到内建函数<strong>iter</strong>，简单理解为，使用了<strong>iter</strong>才会是一个可迭代对象，关于这部分，我们可以对一些对象做一些判断，从而清楚是不是可迭代对象，比如L1列表：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">from collections.abc import Iterable</span><br><span class="line"># 判断L1是否为可迭代对象</span><br><span class="line">L1 &#x3D; [i for i in range(4)]</span><br><span class="line">print(isinstance(L1, Iterable)) # 结果为True，确定为迭代对象</span><br></pre></td></tr></table></figure>
<p>&emsp;&emsp;这里我们使用<strong>iter</strong>()方法和<strong>next</strong>()方法来将元素取出。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">L2 &#x3D; L1.__iter__()</span><br><span class="line"># L2 &#x3D; iter(L1) # 和上面一句等价</span><br><span class="line">print(next(L2))</span><br><span class="line">print(L2.__next__()) # 和上面一句等价</span><br><span class="line">print(next(L2))</span><br></pre></td></tr></table></figure>
<p>&emsp;&emsp;在这里我们实现了使用for循环来取出元素的方法，在了解这里之后，我们开始做一个自己的迭代器</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"># 构造迭代器</span><br><span class="line">class NewIter():</span><br><span class="line">    # 用于生成斐波那契数列</span><br><span class="line">    def __init__(self, num):</span><br><span class="line">        self.num &#x3D; num</span><br><span class="line">        self.a &#x3D; 0</span><br><span class="line">        self.b &#x3D; 1</span><br><span class="line"></span><br><span class="line">    def __iter__(self):</span><br><span class="line">        # 创建可迭代对象</span><br><span class="line">        return self</span><br><span class="line">    </span><br><span class="line">    def __next__(self):</span><br><span class="line">        while self.num &gt; 0:</span><br><span class="line">            result &#x3D; self.a</span><br><span class="line">            self.a, self.b &#x3D; self.b, self.a + self.b</span><br><span class="line">            self.num -&#x3D; 1</span><br><span class="line">            return result</span><br><span class="line">        # 循环结束后跳出</span><br><span class="line">        raise StopIteration</span><br><span class="line">        </span><br><span class="line">new_iter &#x3D; NewIter(5)</span><br><span class="line">print(next(new_iter))</span><br><span class="line">print(next(new_iter))</span><br></pre></td></tr></table></figure>
<h2 id="生成器"><a href="#生成器" class="headerlink" title="生成器"></a>生成器</h2><p>&emsp;&emsp;这里引入生成器个概念，生成器能做到迭代器能做的所有事，由于自动创建了iter()和next()方法，生成器显得特别简洁。除了创建和保存程序状态的自动方法,当<br>发生器终结时,还会自动抛出 StopIteration 异常。简单理解他就是迭代器的一种！！使用yield即可实现。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"># 生成器实现</span><br><span class="line">def new_create(num):</span><br><span class="line">    # 用于生成斐波那契数列</span><br><span class="line">    a, b &#x3D; 0, 1</span><br><span class="line">    while num &gt; 0:</span><br><span class="line">        yield a</span><br><span class="line">        a, b &#x3D; b, a+b</span><br><span class="line">        num -&#x3D; 1</span><br><span class="line">        </span><br><span class="line">new_iter &#x3D; new_create(5)</span><br><span class="line">print(next(new_iter))</span><br><span class="line">print(next(new_iter))</span><br></pre></td></tr></table></figure>
<h2 id="结语"><a href="#结语" class="headerlink" title="结语"></a>结语</h2><p>&emsp;&emsp;目的就是节省空间，提高复用率</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/06/02/Python%E8%BF%AD%E4%BB%A3%E5%99%A8%E5%8F%8A%E7%94%9F%E6%88%90%E5%99%A8%E7%9A%84%E7%AE%80%E5%8D%95%E7%90%86%E8%A7%A3/" data-id="ckax72g0g00006bbib25q14dw" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2020/06/07/Python%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%93numpy%E7%9A%84%E7%AE%80%E5%8D%95%E5%85%A5%E9%97%A8%E4%BD%BF%E7%94%A8/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Python第三方库numpy的简单入门使用
        
      </div>
    </a>
  
  
    <a href="/2020/06/01/Python%E8%A3%85%E9%A5%B0%E5%99%A8%E7%9A%84%E7%AE%80%E5%8D%95%E7%90%86%E8%A7%A3/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Python装饰器的简单理解</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/06/">六月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/05/">五月 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2020/06/07/Python%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%93numpy%E7%9A%84%E7%AE%80%E5%8D%95%E5%85%A5%E9%97%A8%E4%BD%BF%E7%94%A8/">Python第三方库numpy的简单入门使用</a>
          </li>
        
          <li>
            <a href="/2020/06/02/Python%E8%BF%AD%E4%BB%A3%E5%99%A8%E5%8F%8A%E7%94%9F%E6%88%90%E5%99%A8%E7%9A%84%E7%AE%80%E5%8D%95%E7%90%86%E8%A7%A3/">Python迭代器及生成器的简单理解</a>
          </li>
        
          <li>
            <a href="/2020/06/01/Python%E8%A3%85%E9%A5%B0%E5%99%A8%E7%9A%84%E7%AE%80%E5%8D%95%E7%90%86%E8%A7%A3/">Python装饰器的简单理解</a>
          </li>
        
          <li>
            <a href="/2020/06/01/Python%E9%97%AD%E5%8C%85%E7%9A%84%E7%AE%80%E8%A6%81%E4%BB%8B%E7%BB%8D/">闭包的简要介绍</a>
          </li>
        
          <li>
            <a href="/2020/05/31/Python%E8%BF%9E%E6%8E%A5C%E5%AE%9E%E7%8E%B0%E9%AB%98%E6%95%88%E7%BC%96%E7%A8%8Bdemo/">Python连接C实现高效编程demo</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>